/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trinomialtree.input;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class RateInputReader {

    private String inputFilePath;

    public RateInputReader(String path) {
        this.inputFilePath = path;
    }

    public List readAvailableDates() throws IOException, ParseException {

        List<String> list = new ArrayList<String>();


        //Wir haben drei Referenzdatumswerte, die wir aber nicht dynamisch irgendwo auslesen
        list.add("2003-09-01 EURIBOR");
        list.add("1998-01-01 EURIBOR");
        list.add("1998-01-01 LIBOR");
        list.add("Testkurve");


//		// Öffne das input file
//		BufferedReader input = new BufferedReader(new FileReader(this.inputFilePath));
//		String line = null;
//
//		int lineCount = 0;
//
//		while ((line = input.readLine()) != null) {
//			StringTokenizer tokenizer = new StringTokenizer(line, ";");
//
//			if (lineCount == 0) {
//				//In der Zeile stehen nur die Überschriften (Datum)
//
//
//				while (tokenizer.hasMoreTokens()) {
//					//Erfasse die Datumswerte für die Rates verfügbar sind
//					list.add(tokenizer.nextToken());
//
//				}
//			}
//			else {
//				break;
//			}
//			lineCount++;
//		}
//
//		input.close();

        return list;

    }

    public ArrayList getRates(int date, int granularity) throws IOException, ParseException {

        ArrayList<Double> rateInputs = new ArrayList<Double>();

        String filename = "";

        //Wöchentlich
        if (granularity == 0) {

            //EURIBOR 2003 - 2013
            if (date == 0) {
                filename = "EURIBOR_weekly_2003_2012.csv";
            } //EURIBOR 1999 - 2010
            else if (date == 1) {
                filename = "EURIBOR_weekly_1999_2010.csv";
            } //LIBOR 1999 - 2010
            else if (date == 2) {
                filename = "LIBOR_weekly_1999_2010.csv";
            } //EURIBOR 1998
            else if (date == 3) {
                filename = "EURIBOR_weekly_1998.csv";
            }


        } //Monatlich
        else if (granularity == 1) {

            //EURIBOR 2003 - 2013
            if (date == 0) {
                filename = "EURIBOR_monthly_2003_2012.csv";
            } //EURIBOR 1999 - 2010
            else if (date == 1) {
                filename = "EURIBOR_monthly_1999_2010.csv";
            } //LIBOR 1999 - 2010
            else if (date == 2) {
                filename = "LIBOR_monthly_1999_2010.csv";
            }
            else if (date == 3) {
                filename = "EURIBOR_monthly_1998.csv";
            }


        } //Jährlich
        else if (granularity == 2) {

            //EURIBOR 2003 - 2013
            if (date == 0) {
                filename = "EURIBOR_yearly_2003_2012.csv";
            } //EURIBOR 1999 - 2010
            else if (date == 1) {
                filename = "EURIBOR_yearly_1999_2010.csv";
            } //LIBOR 1999 - 2010
            else if (date == 2) {
                filename = "LIBOR_yearly_1999_2010.csv";
            }
            else if (date == 3) {
                filename = "EURIBOR_yearly_1998.csv";
            }


        }



        // Öffne das input file
        BufferedReader input = new BufferedReader(new FileReader(this.inputFilePath + filename));


        String line = null;


        while ((line = input.readLine()) != null) {

            StringTokenizer tokenizer = new StringTokenizer(line, ";");

            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                rateInputs.add(getDouble(token));
            }

        }



        input.close();



        return rateInputs;
    }

    public ArrayList<Double> getSwapRates(int date, int granularity, int laufzeitOption) throws IOException, ParseException {

        ArrayList<Double> rateInputs = new ArrayList<Double>();

        String filename = "";

        if (date == 0) {
            filename = "EURIBOR_monthly_2003_2012.csv";
        } else if (date == 1) {
            filename = "EURIBOR_monthly_1999_2010.csv";
        } else if (date == 2) {
            filename = "LIBOR_monthly_1999_2010.csv";
        }
        else if (date == 3) {
            filename = "EURIBOR_monthly_1998.csv";
        }



        // Öffne das input file
        BufferedReader input = new BufferedReader(new FileReader(this.inputFilePath + filename));
        String line = null;

        List<Double> monthlyRates = new ArrayList<Double>();
        while ((line = input.readLine()) != null) {
            monthlyRates.add(getDouble(line));
        }

        int startPunkt = laufzeitOption * 12 - 1;
        if (granularity == 2) {
            rateInputs.add(monthlyRates.get(startPunkt));
            rateInputs.add(monthlyRates.get(startPunkt+12));
        }
        else if (granularity == 4) {
            rateInputs.add(monthlyRates.get(startPunkt));
            rateInputs.add(monthlyRates.get(startPunkt+=4));
            rateInputs.add(monthlyRates.get(startPunkt+=4));
            rateInputs.add(monthlyRates.get(startPunkt+=4));
        }


        input.close();



        return rateInputs;
    }

    public static Double getDouble(String s) {

        try {
            return new Double(s.replaceAll(",", "."));
        } catch (Exception e) {
            return null;
        }


    }
}
